<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span> 管理系统</span>
      </div>
      <el-button type="info" @click="Logout" class="exit_btn">退出</el-button>
    </el-header>
    <el-container class="home_body">
      <!-- 侧边栏 -->
      <el-aside :width="isshow ? '64px':'200px'">
        <div class="toggle-btn" @click="toggleMenu">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu :collapse='isshow' :collapse-transition="false" background-color= "#333744" text-color="#fff" active-text-color="#409EFF" :unique-opened='true' :router="true" :default-active="NavState">
          <el-submenu v-for="item in MenuList" :key="item.id" :index="item.id+''">
            <template slot="title"><i :class="iconsObj[item.id]"></i> <span>{{item.authName}}</span></template>
          <el-menu-item v-for="subitem in item.children" :key="subitem.id" :index="NavState" @click="saveNavState('/'+subitem.path)">
            <i class="el-icon-menu"></i><span>{{subitem.authName}}</span>
          </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  created () {
    this.getMenuList()
    this.NavState = window.sessionStorage.getItem('NavState')
  },
  data () {
    return {
      // 左侧菜单数据
      MenuList: [],
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 菜单栏显示折叠
      isshow: false,
      // 被激活的链接地址
      NavState: ''
    }
  },
  methods: {
    // 退出登录
    Logout: function () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.MenuList = res.data
    },
    toggleMenu: function () {
      this.isshow = !this.isshow
    },
    saveNavState: function (params) {
      window.sessionStorage.setItem('NavState', params)
      this.NavState = params
    }
  }
}
</script>
<style lang="less" scoped>
.home_container{
  height: 100%;

}
.el-header{
  background-color: #373d41;
  color: #fff;
  display:flex;
  justify-content:space-between;
  padding-left: 0;
  align-items: center;
  font-size:20px;
  >div{
    display:flex;
    align-items: center;
    span{
      margin-left: 10px;
    }
  }
}
.el-aside{
  background-color: #333744;
  .toggle-btn{
    background-color: #4A5064;
    color: #fff;
    text-align: center;
    font-size: 10px;
    line-height: 20px;
    letter-spacing: .2em;
    cursor: pointer;
  }
  .el-menu{
    border:none
  }
}
.el-main{
  background-color: #fff;
}

</style>
